package service

import (
	"book-hub/book-service/basic/handler/model"
	__ "book-hub/book-service/basic/proto"
	"context"
	"gorm.io/gorm"
)

type Service struct {
	__.BookServer
}

func (s Service) DelBook(_ context.Context, in *__.DelBookReq) (*__.DelBookResp, error) {

	var book model.Book
	err := model.DelBook(&book, int(in.ID))
	if err != nil {
		return &__.DelBookResp{
			Code: 0,
			Msg:  "删除失败",
		}, nil
	}
	return &__.DelBookResp{
		Code: 200,
		Msg:  "删除成功",
	}, nil
}

func (s Service) UpdateBook(_ context.Context, in *__.UpdateBookReq) (*__.UpdateBookResp, error) {

	var book model.Book
	book = model.Book{
		Model: gorm.Model{
			ID: uint(in.ID),
		},
		Type: in.Type,
	}
	err := model.Update(&book, int(book.ID))
	if err != nil {
		return &__.UpdateBookResp{
			Code: 0,
			Msg:  "修改失败",
		}, nil
	}
	return &__.UpdateBookResp{
		Code: 200,
		Msg:  "修改成功",
	}, nil
}

func (s Service) FindBook(_ context.Context, in *__.FindBookReq) (*__.FindBookResp, error) {

	var book model.Book
	err := model.BookFind(&book, in.Name)
	if err != nil {
		return nil, err
	}
	return &__.FindBookResp{
		Name:    book.Name,
		Status:  book.Status,
		Author:  book.Author,
		People:  book.People,
		Address: book.Address,
		Year:    book.Year,
		Price:   float32(book.Price),
		SjName:  book.SjName,
		Num:     int64(book.Num),
		UserId:  int64(book.UserId),
	}, nil
}

func (s Service) ListBook(_ context.Context, in *__.ListBookReq) (*__.ListResp, error) {

	var book model.Book
	err := model.FindBook(&book, int(in.Page), int(in.Size))
	if err != nil {
		return nil, err
	}
	return &__.ListResp{
		Name:    book.Name,
		Status:  book.Status,
		Author:  book.Author,
		People:  book.People,
		Address: book.Address,
		Year:    book.Year,
		Price:   float32(book.Price),
		SjName:  book.SjName,
		Num:     int64(book.Num),
		UserId:  int64(book.UserId),
	}, nil

}

func (s Service) CreateBook(_ context.Context, in *__.CreateBookReq) (*__.CreateBookResp, error) {

	var book model.Book

	if in.Name != "" || in.Num < 1 {
		return &__.CreateBookResp{
			Code: 0,
			Msg:  "图书名不能为空或库存不能小于1",
		}, nil
	}
	book = model.Book{
		UserId:  int(in.UserId),
		Name:    in.Name,
		Status:  in.Status,
		Author:  in.Author,
		People:  in.People,
		Address: in.Address,
		Year:    in.Year,
		Price:   float64(in.Price),
		SjName:  in.SjName,
		Num:     int(in.Num),
	}
	err := model.CreateBook(&book)
	if err != nil {
		return &__.CreateBookResp{
			Code: 0,
			Msg:  "添加失败",
		}, nil
	}
	return &__.CreateBookResp{
		Code: 200,
		Msg:  "添加成功",
	}, nil
}
func (s Service) UserLogin(_ context.Context, in *__.UserLoginReq) (*__.UserLoginResp, error) {

	var user model.User
	user = model.User{

		Username: in.Username,
		Password: in.Password,
	}
	err := model.UserLogin(&user, in.Username)
	if err != nil {
		return &__.UserLoginResp{
			Code: 0,
			Msg:  "登录失败",
		}, nil
	}
	return &__.UserLoginResp{
		Code: 200,
		Msg:  "登录成功",
	}, nil
}

func (s Service) LoginUser(_ context.Context, in *__.LoginUserReq) (*__.LoginUserResp, error) {

	var user model.User
	user = model.User{
		Image:    in.Image,
		Username: in.Username,
		Password: in.Password,
		Number:   int(in.Number),
	}
	err := model.LoginUser(&user)
	if err != nil {
		return &__.LoginUserResp{
			Code: 0,
			Msg:  "注册失败",
		}, nil
	}
	return &__.LoginUserResp{
		Code: 200,
		Msg:  "注册成功",
	}, nil
}
